﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;
using HRM.DAL.Report;

namespace HRM.UI.HRManagerUI.EmpModule
{
    public partial class AddPmManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadProject();
                PopulateProjectManager();
            }

        }

        private void LoadProject()
        {
            List<Project> projectlist = new List<Project>();
            ProjectManager PmManager = new ProjectManager();
            projectlist = PmManager.GetAllProjects();
            ddProjectList.DataTextField = "ProjectName";
            ddProjectList.DataValueField = "Id";
            ddProjectList.DataSource = projectlist;
            ddProjectList.DataBind();
            ddProjectList.Items.Insert(0, "---Choose Any---");

        }

        protected void SaveManager(object sender, EventArgs e)
        {
            try
            {
                Employee employee = new Employee();
                Code code = new Code();
                code.CodeName = txtEmployeeCodepm.Value;
                employee.EmpCode = code;

                Project project = new Project();
                project.Id = Convert.ToInt32(ddProjectList.Value);

                PM aPmManager = new PM();
                PmManager manager = new PmManager();

                aPmManager.Employee = employee;
                aPmManager.Project = project;
                aPmManager.ManagerCreateDate = Convert.ToDateTime(txtDatepm.Value);
                if (manager.SaveProjectManager(aPmManager))
                {
                    ClearInput();
                    MsgLebel.ForeColor = Color.Green;
                    MsgLebel.Text = "Project Manager Saved Successfully";
                    PopulateProjectManager();
                }
                else
                {

                    MsgLebel.ForeColor = Color.Red;
                    MsgLebel.Text = "Sorry Project Manager not saved";

                }


            }
            catch (Exception ex)
            {

                MsgLebel.ForeColor = Color.Red;
                MsgLebel.Text = ex.Message;
            }

        }

        protected void ClearInput()
        {
            txtEmployeeCodepm.Value = "";
            ddProjectList.Value = "";
            txtDatepm.Value = "";
        }

        protected void PopulateProjectManager()
        {
            PmManager pmManager = new PmManager();
            List<ViewProjectManager> pmlist = new List<ViewProjectManager>();
           // ViewProjectManager viewProjectManager = new ViewProjectManager();
            pmlist = pmManager.GetProjectManagers();
            PmManagerGdv.DataSource = pmlist;
            PmManagerGdv.DataBind();



        }

        protected void PmManagerGdv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            PmManagerGdv.PageIndex = e.NewPageIndex;
            PopulateProjectManager();
        }

        protected void PmManagerGdv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
             try
            {
                PmManager pmManager=new PmManager();
                int Id = Convert.ToInt32(((Label)PmManagerGdv.Rows[e.RowIndex].FindControl("lblId")).Text);
                string msg = pmManager.DeletePm(Id);
                if (msg.Contains("Successfully"))
                {
                    LblDeletePm.Text = "Punished Type Successfully deleted";
                    LblDeletePm.ForeColor = Color.Green;
                    PopulateProjectManager();

                }
                else
                {
                    LblDeletePm.Text = "sorry !Punished Type not deleted";
                    LblDeletePm.ForeColor = Color.Red;
                }

            }
            catch (Exception)
            {
                LblDeletePm.Text = "sorry !Punished Type not deleted";
                LblDeletePm.ForeColor = Color.Red;
            }
        }

        }

    }
